<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');


/**
 * This class is for online shop order process - payment.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */
 
 /**
 * Start page
 */
session_start();

$odrSumary = new OrderSumary();
$odrSumary->run();

class OrderSumary extends Controller
{	
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();
		header('Cache-Control: no-store, no-cache, must-revalidate');
		require_once('system/libraries/currency.php');
		$this->obj_currency = new Currency();
	}
	
	/** 
	 * Start page function
	 * @params null
	 * @Return null
	 */
	public function run()
	{
		if (FE_USER_LOGGED_IN)
		{			
			if (!isset($_GET['id_order']) || !$_GET['id_order'])
			{
				header("Location:id-4o4-page.html");exit;
			}
			
			$this->import('Database');
			
			if (!$_POST)
				echo $this->displayOderSummary(trim($_GET['id_order']));
			else
			{
				
			}
		}
		else
		{
			$this->redirect("sign-in.html");
		}
	}
	
	/**
	 * Generate the order summary
	 * @params string
	 * @Return string
	 */
	private function displayOderSummary($id_order=0)
	{
		if (!$id_order)
			return;
		
		$this->import("FrontendUser", "User");
		$this->loadDataContainer("tl_member");
		$countries = array();
		$objOders = $this->Database->prepare("SELECT IF(odr.dly_firstname <> '', odr.dly_firstname, addrbook.firstname) AS firstname, IF(odr.dly_lastname <> '', odr.dly_lastname, addrbook.lastname) AS lastname, IF(odr.dly_street <> '', odr.dly_street, addrbook.street) AS street, IF(odr.dly_street_2 <> '', odr.dly_street_2, addrbook.street_2) AS street_2, IF(odr.dly_city <> '', odr.dly_city, addrbook.city) AS city_shipping, IF(odr.dly_state <> '', odr.dly_state, addrbook.state) AS state_shipping, IF(odr.dly_country <> '',odr.dly_country, addrbook.country) AS country, IF(odr.dly_postcode <> '', odr.dly_postcode, addrbook.postcode) AS postcode, IF(odr.dly_cellphone <> '', odr.dly_cellphone, addrbook.cellphone) AS cellphone, 
		IF(odr.bill_firstname <> '', odr.bill_firstname, addrbook_bill.firstname) AS firstname_bill, IF(odr.bill_lastname <> '', odr.bill_lastname, addrbook_bill.lastname) AS lastname_bill, IF(odr.bill_street <> '', odr.bill_street, addrbook_bill.street) AS street_bill, IF(odr.bill_street_2 <> '', odr.bill_street_2, addrbook_bill.street_2) AS street_2_bill, IF(odr.bill_city <> '', odr.bill_city, addrbook_bill.city) AS city_bill, IF(odr.bill_state <> '', odr.bill_state, addrbook_bill.state) AS state_bill, IF(odr.bill_country <> '', odr.bill_country, addrbook_bill.country) AS country_bill, IF(odr.bill_postcode <> '', odr.bill_postcode, addrbook_bill.postcode) AS postcode_bill, IF(odr.bill_cellphone <> '', odr.bill_cellphone, addrbook_bill.cellphone) AS cellphone_bill, 
								odd.product_price AS odrPrice, odr.payment AS paymethod, odr.total_discounts, odr.orderno AS orderno, odr.total_wrapping, odr.odr_status,
								odr.total_catolog, odr.total_paid_real, odr.total_paid, odr.date_add AS placed_time, odr.total_shipping AS shipping_fee, odr.`shipping_id` AS ship_id, 
								odr.total_catolog AS product_fee, odd.product_attribute AS product_attr, prod_cata.skuid AS skuid, prod_cata.category AS category, prod_cata.id AS id_catalog, prod_cata.name AS catalogName, 
								odd.product_quantity AS prod_quantity, odd.is_package AS is_wrapping, odd.gwPrice AS wrapping_price,
								odr_sta.name as order_status, cur.iso_code 
								FROM tl_orders AS odr
								LEFT JOIN tl_order_detail AS odd ON odd.pid=odr.id
								LEFT JOIN tl_address_book AS addrbook ON addrbook.id=odr.id_address_delivery
								LEFT JOIN tl_address_book AS addrbook_bill ON addrbook_bill.id=odr.id_address_invoice
								LEFT JOIN product_catalog AS prod_cata ON prod_cata.id=odd.product_catalog_id
								LEFT JOIN tl_order_state_lang AS odr_sta ON odr.odr_status=odr_sta.id_order_state AND odr_sta.id_lang=1
								LEFT JOIN tl_currency AS cur ON cur.id=odr.id_currency
								WHERE odr.id=?")
						->execute($id_order);
		
		if ($objOders->numRows < 1)
		{
			header("Location:id-4o4-page.html");exit;
		}
			
		$this->loadLanguageFile('countries');
		include(TL_ROOT . '/system/config/countries.php');
		
		// Get the states.
		$arrStates = array();
		$objStates = $this->Database->prepare("SELECT * FROM `china_province` ORDER BY `province_en`")
									->execute(time());
		
		while ($objStates->next())
		{
			$arrStates[$objStates->id] = stripslashes($objStates->province_en);
		}
		
		// Get State.
		$arrCitys = array();			
		$objCitys = $this->Database->prepare("SELECT * FROM `china_city` ORDER BY `city_en`")
									->execute(time());
									
		while ($objCitys->next())
		{
			$arrCitys[$objCitys->id] = stripslashes($objCitys->city_en);
		}	
		

		if($objOders->street_2=="line 2" || $objOders->street_2=="Address line 2") {
			$street = $objOders->street . ", ";
		}else{
			$street = $objOders->street . "<br />" .$objOders->street_2. ", ";
		}
		
		if($objOders->street_2_bill=="line 2" || $objOders->street_2_bill=="Address line 2") {
			$street_bill = $objOders->street_bill . ", ";
		}else{
			$street_bill = $objOders->street_bill . "<br />" .$objOders->street_2_bill. ", ";
		}
		
		$arr_shippings = array();
		if ($objOders->ship_id)
		{
			$obj_select_shipping = $this->Database->prepare("SELECT * FROM `tl_shippings` WHERE `id`=?")
													->execute($objOders->ship_id);
			$arr_shippings = array(
				'name'=>$obj_select_shipping->name,
				'ship_des'=>$obj_select_shipping->ship_des,
			);
		}
		
		$strHtml = "<div class=\"path\"><a title=\"HOME\" href=\"index.php\">HOME</a>&nbsp;" . htmlentities('>') . "&nbsp;<a title=\"MY ACCOUNT\" href=\"profile.html\">" . (($this->User->firstname) ? $this->User->firstname : $this->User->email) . "'s account</a></div><div class=\"quick_tool\">
					<a title=\"ORDER STATUS\" class=\"active\" href=\"order-status.html\">ORDER STATUS</a> <a href=\"profile_detail.html\" title=\"PROFILE\">PROFILE</a> <a title=\"WISHLIST\" href=\"backpocket.html\">WISHLIST</a> <a title=\"ADDRESS BOOK\" href=\"shipping_payment.html\">ADDRESS BOOK</a>
				</div>
	<form id=\"form_odr_payment\" action=\"orderPayment.html\" name=\"order_payment\" method=\"post\">
	<div id=\"order_summery\">
		<h1>ORDER #" . $objOders->orderno . " <small>(Placed " . date('m/d/Y', strtotime($objOders->placed_time)) . ")<br/>
		".(($objOders->order_status == 'Payment accepted' && $objOders->paymethod == 'COD') ? 'Preparation in Progress' : $objOders->order_status) . "
		</small></h1>
		<ul class=\"order_method\">
			<li>
				<h4>BILLING ADDRESS</h4>
				<p>" . $objOders->firstname_bill . " " . $objOders->lastname_bill . ", <br />" . $street_bill . (($arrCitys[$objOders->city_bill]&&$objOders->country_bill=='cn') ? $arrCitys[$objOders->city_bill] : $objOders->city_bill) . "<br />" . (($arrStates[$objOders->state_bill]&&$objOders->country_bill=='cn') ? $arrStates[$objOders->state_bill] : $objOders->state_bill) . "&nbsp;" . $objOders->postcode_bill . "&nbsp;" . $countries[$objOders->country_bill] . "</p>
			</li>
			<li>
				<h4>SHIPPING ADDRESS</h4>
				<p>" . $objOders->firstname . " " . $objOders->lastname . ", <br />" . $street . (($arrCitys[$objOders->city_shipping]&&$objOders->country=='cn') ? $arrCitys[$objOders->city_shipping] : $objOders->city_shipping) . "<br />" . (($arrStates[$objOders->state_shipping]&&$objOders->country=='cn') ? $arrStates[$objOders->state_shipping] : $objOders->state_shipping) . "&nbsp;" . $objOders->postcode . "&nbsp;" . $countries[$objOders->country] . "</p>
			</li>
			<li>
				<h4>DEFAULT SHIPPING METHOD</h4>
				" . (($arr_shippings) ? "<p>".$arr_shippings['name']." ".$this->obj_currency->change_order($objOders->shipping_fee,$objOders->iso_code)."</p>" . $arr_shippings['ship_des'] : (($objOders->shipping_fee == "10.00") ? "<p>STANDARD DELIVERY: ￥10.00</p><p><small>Your package will arrive within 2~3 days.</small></p>" : (($objOders->shipping_fee == "0.00") ? "￥ 0.00 - free shopping till August 31" : "<p>SAME DAY DELIVERY (SHANGHAI ONLY): ￥25.00</p><p><small>Your package will arrive within 24 hours after order is placed</small></p>"))) . "
			</li>
			<li>
				<h4>DEFAULT PAYMENT METHOD</h4>
				<p>" . (($objOders->paymethod == 'COD') ? "Cash on delivery (COD)" : $objOders->paymethod) . "</p>" . 
			(($objOders->paymethod != 'COD' && $objOders->odr_status < 2) ? "
				<script type=\"text/javascript\">
					jQuery(function(){
						jQuery(\"#re_payment\").find('a').click(function(e){
							jQuery(\"#form_odr_payment\").submit();
							return false;
						});
					});
				</script>
				<p id=\"re_payment\"><a href=\"javascript:void(0);\" style=\"color:Red;font-style:italic\">Problem with making payment?<br />Try again</a></p>" : "") . "
			</li>
			<!--<li>
				<h4>DEFAULT PAYMENT METHOD</h4>
				<p>MASTERCARD</p>
				<p>XXXX-XXXX-XXXX-9073(06-2010)</p>
			</li>-->
		</ul> <!-- order_method end -->
		<h3>ORDER SUMMERY</h3>";
		
		$catalogFree = 0.00;
		$shippingFree = $objOders->shipping_fee;
		$total_discounts = $objOders->total_discounts;
		
		do
		{
			$arrColor = explode("\n", $objOders->product_attr);
			$arrColor_bk = explode(":", $arrColor[0]);
			$strColor = $arrColor_bk[1];
			$arrSize_bk = explode(":", $arrColor[1]);
			$strSize = $arrSize_bk[1];
			$objCatalogSize = $this->Database->prepare("SELECT `id` FROM `catalog_size` WHERE `size`=?")
							->execute($strSize);
			$objCatalogColor = $this->Database->prepare("SELECT `id` FROM `catalog_colour` WHERE `title`=?")
							->execute($strColor);
			$imgPath = "daft_files/catalog/products/" . $objOders->skuid . "/$strColor/1.jpg";
			$unitWrapping = $objOders->wrapping_price;
			if ($objOders->is_wrapping)
				$detailTotal = number_format(($objOders->odrPrice * $objOders->prod_quantity + $objOders->total_wrapping), 2, '.', '');
			else
				$detailTotal = number_format(($objOders->odrPrice * $objOders->prod_quantity), 2, '.', '');
			
			$catalogFree += $detailTotal;
			$strHtml .= "<table class=\"cart_item\">
			<tr>
				<td rowspan=\"2\" class=\"td_pic\"><a href=\"./product.html?category=".$objOders->category."&id=". $objOders->id_catalog . "&id_colour=" . $objCatalogColor->id . "&id_size=" . $objCatalogSize->id . "\"><img src=\"image.php?src=" . $imgPath . "&width=79&height=79\" alt=\"\" /></a></td>
				<td class=\"td_info\">
					<h4><a href=\"./product.html?category=".$objOders->category."&id=". $objOders->id_catalog . "&id_colour=" . $objCatalogColor->id . "&id_size=" . $objCatalogSize->id . "\">" . $objOders->catalogName . "</a></h4>
					<small>SKU #" . $objOders->skuid . "</small><br />
					<small>" . $this->obj_currency->change_order(number_format($objOders->odrPrice, 2, '.', ''),$objOders->iso_code)  . "</small>" . (($objOders->is_wrapping) ? "<br /><small>" . $this->obj_currency->change_order(number_format($unitWrapping, 2, '.', ''),$objOders->iso_code) . "</small>" : "")
			 . "
				</td>
				<td rowspan=\"2\" class=\"td_skin\">
					<div class=\"skin\">
						<p>
							<label>COLOR</label>
							<span class=\"skin_value\">" . $strColor . "</span>
						</p>
						<p>
							<label>SIZE</label>
							<span class=\"skin_value\">" . $strSize . "</span>
						</p>
						<p>
							<label>QUANTITY</label>
							<span class=\"skin_value\">" . $objOders->prod_quantity . "</span>
						</p>
					</div>
				</td>
				<td rowspan=\"2\" class=\"unit_price\"><strong>" . $this->obj_currency->change_order($detailTotal,$objOders->iso_code) . "</strong></td>
			</tr>
		</table>";
		}
		while ($objOders->next());
		
		$strHtml .= "<!--<div class=\"cancel_order\">
			<form name=\"cancel_order\" action=\"" . $this->Environment->request . "\" method=\"post\">
				<input type=\"hidden\" name=\"SUBMIT_FORM\" value=\"cancel_order\" />
				<input type=\"submit\" name=\"btn_submit\" value=\"Cancel Order\" />
			</form>
		</div>-->
		<div class=\"count_price\">
			<p>SUBTOTAL: <span>" . $this->obj_currency->change_order(number_format($catalogFree, 2, '.', ''),$objOders->iso_code) . "</span></p>
			<p>SHIPPING: <span>" . $this->obj_currency->change_order(number_format($shippingFree, 2, '.', ''),$objOders->iso_code) . "</span></p>
			" . (($total_discounts > 0) ? ("<p class=\"order_givevoucher\">GIFT VOUCHER: <span>- " . $this->obj_currency->change_order($total_discounts,$objOders->iso_code) ."</span></p>") : "<p class=\"order_givevoucher\">DISCOUNT<span>: " . $this->obj_currency->change_order(0,$objOders->iso_code) . "</span></p>") . "
		</div> <!-- order_price end -->
		<div class=\"total_price\">
			<small>TOTAL //</small>
			<span><span id=\"total_cost\">" . $this->obj_currency->change_order(number_format(($catalogFree + $shippingFree - $total_discounts), 2, '.', ''),$objOders->iso_code) . "</span></span>
		</div> <!-- total_price end -->
	</div> <!-- order_summery end -->
	<input type=\"hidden\" name=\"id_order\" value=\"$id_order\" />
	</form>";
	
		return $strHtml;
	}
	
}
?>